/*
 *  Copyright (C) 2010 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_Config.java
 *
 * Created on 28/07/2010, 10:01:56
 */
package kuasar.util.config;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Stack;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import kuasar.util.Files;
import kuasar.util.Error;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_Config extends kuasar.plugin.classMod.AbstractPanel {

    private kuasar.gui.fun_frm_Main parent;

    /** Creates new form pn_Config */
    public pn_Config(kuasar.gui.fun_frm_Main parent) {
        this.parent = parent;
        initComponents();
        spn_Main.getViewport().setOpaque(false);
        loadData();
        parent.invisibleToolBar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        spn_Main = new javax.swing.JScrollPane();
        pn_Main = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cmb_Language = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cmb_Skins = new javax.swing.JComboBox();
        btn_Cancel = new javax.swing.JButton();
        btn_Save = new javax.swing.JButton();

        setOpaque(false);

        spn_Main.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_Main.setViewportBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        spn_Main.setOpaque(false);

        pn_Main.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        pn_Main.setOpaque(false);
        pn_Main.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                pn_MainComponentMoved(evt);
            }
        });

        jLabel3.setForeground(new java.awt.Color(204, 204, 204));
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/configure.png"))); // NOI18N
        jLabel3.setText("Preferences");

        jLabel2.setForeground(new java.awt.Color(204, 204, 204));
        jLabel2.setText("Language:");

        cmb_Language.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "English" }));

        jLabel1.setForeground(new java.awt.Color(204, 204, 204));
        jLabel1.setText("Skin:");

        btn_Cancel.setText("Cancel");
        btn_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CancelActionPerformed(evt);
            }
        });

        btn_Save.setText("Save");
        btn_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pn_MainLayout = new javax.swing.GroupLayout(pn_Main);
        pn_Main.setLayout(pn_MainLayout);
        pn_MainLayout.setHorizontalGroup(
            pn_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_MainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pn_MainLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(cmb_Skins, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                    .addGroup(pn_MainLayout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmb_Language, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_MainLayout.createSequentialGroup()
                        .addComponent(btn_Save)
                        .addGap(18, 18, 18)
                        .addComponent(btn_Cancel))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        pn_MainLayout.setVerticalGroup(
            pn_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_MainLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pn_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmb_Language, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmb_Skins, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addGroup(pn_MainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Cancel)
                    .addComponent(btn_Save))
                .addContainerGap())
        );

        spn_Main.setViewportView(pn_Main);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(spn_Main, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(spn_Main, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void pn_MainComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_pn_MainComponentMoved
        parent.updateUI();
    }//GEN-LAST:event_pn_MainComponentMoved

    private void btn_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CancelActionPerformed
        parent.unLoadPlugin();
    }//GEN-LAST:event_btn_CancelActionPerformed

    private void btn_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SaveActionPerformed

        File file = new File(Configuration.startDir, Configuration.cfgFile);
        try {
            if (!file.exists()) {
                if (!file.createNewFile()) {
                    Error.graphicError("Creation file error", "Preferences File can't be created. Check permissions!");
                    return;
                }
            }
            PrintWriter pwfile = new PrintWriter(new FileWriter(file, false));

            for (String data : recopileData()) {
                pwfile.println(data);
            }

            pwfile.close();
        } catch (IOException ex) {
            kuasar.util.Error.console(this.getClass().getSimpleName() + "btn_SaveActionPerformed(java.awt.event.ActionEvent evt)", ex.getMessage());
            return;
        }
        parent.launchInfo("Preferences have been saved!");
        parent.unLoadPlugin();
    }//GEN-LAST:event_btn_SaveActionPerformed

    private String[] recopileData() {
        Stack<String> data = new Stack<String>();
        data.add("i18n=" + cmb_Language.getSelectedItem().toString());
        data.add("skin=" + ((Object[]) cmb_Skins.getSelectedItem())[0].toString());
        return data.toArray(new String[0]);
    }
    
    private void loadData() {
        HashMap info = Files.loadMap(Configuration.startDir + Configuration.cfgFile);
       loadSkins(info);
    }
   
    private void loadSkins(HashMap info){
        String path = Configuration.startDir + Configuration.Interfice.pathskins + File.separator;
        cmb_Skins.setRenderer(new kuasar.classMod.Cmb_Skins_CellRenderer());
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        cmb_Skins.setModel(model);
        int i =0;
        int selected =0;
        for (String dir : Files.getDirs(path)) {
            if(dir.equals(info.get("skin"))){
                selected = i;
            }
            i++;
            Object data[] = new Object[2];
            data[0] = dir;
            if (new File(path + dir + File.separator + Configuration.Interfice.cfgskin).exists()) {
                if (new File(path + dir + File.separator + "screenshoot").exists()) {
                    data[1] = new ImageIcon(path + dir + File.separator + "screenshoot");
                } else {
                    data[1] = null;
                }
            }
            model.addElement(data);
        }
        cmb_Skins.setSelectedIndex(selected);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Cancel;
    private javax.swing.JButton btn_Save;
    private javax.swing.JComboBox cmb_Language;
    private javax.swing.JComboBox cmb_Skins;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel pn_Main;
    private javax.swing.JScrollPane spn_Main;
    // End of variables declaration//GEN-END:variables
}
